package hk.servlet;

import hk.dao.DaoCate;
import hk.dao.impl.DaoCateImpl;
import hk.pojo.AdminInfo;
import hk.pojo.CateInfo;
import hk.pojo.GoodsInfo;
import hk.util.PageInfo;
import hk.util.PageUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/5/16 19:52
 */
@WebServlet("/CateServlet")
public class CateServlet extends HttpServlet {

    private DaoCate daoCate = new DaoCateImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String flag = request.getParameter("flag");
        if("getSubCate".equals(flag)){
            getSubCate(response,request);
        }else if("manager".equals(flag)){
            manager(request,response);
        }
    }

    private void manager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<CateInfo> cateList = daoCate.getCateList(0);
        request.setAttribute("cateList",cateList);
        request.getRequestDispatcher("/goods/goods_menu.jsp").forward(request,response);
    }

    private void getSubCate(HttpServletResponse response, HttpServletRequest request) throws IOException {
        Integer bigcateId = Integer.parseInt(request.getParameter("bigcateId"));
        List<CateInfo> cateList = daoCate.getCateList(bigcateId);
        JSONArray jsonArray = JSONArray.fromObject(cateList);

        response.getWriter().print(jsonArray);
    }
}
